package com.ma5d.driver.driver.service.impl;

import com.ma5d.driver.driver.service.CiService;
import com.ma5d.driver.driver.properties.TencentProperties;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.ciModel.auditing.ImageAuditingRequest;
import com.qcloud.cos.model.ciModel.auditing.ImageAuditingResponse;
import com.qcloud.cos.region.Region;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class CiServiceImpl implements CiService {

    @Resource
    private TencentProperties properties;

    @Override
    public Boolean imageAuditing(String uploadPath) {
        ImageAuditingRequest request = new ImageAuditingRequest();
        request.setBucketName(properties.getBucketPrivate());
        request.setObjectKey(uploadPath);
        COSClient cosClient = this.getCOSClient();
        ImageAuditingResponse response = cosClient.imageAuditing(request);
        cosClient.shutdown();

        if (!response.getPornInfo().getHitFlag().equals("0")
                || !response.getAdsInfo().getHitFlag().equals("0")
                || !response.getTerroristInfo().getHitFlag().equals("0")
                || !response.getPoliticsInfo().getHitFlag().equals("0")
        ) {
            return false;
        }
        return true;
    }

    public COSClient getCOSClient() {
        String secretId = properties.getSecretId();
        String secretKey = properties.getSecretKey();
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        // 2 设置 bucket 的地域, COS 地域
        Region region = new Region(properties.getRegion());
        ClientConfig clientConfig = new ClientConfig(region);
        // 这里建议设置使用 https 协议
        clientConfig.setHttpProtocol(HttpProtocol.https);
        // 3 生成 cos 客户端。
        COSClient cosClient = new COSClient(cred, clientConfig);
        return cosClient;
    }
}
